<?php
if( !defined('CORE') ) exit('Request Error!');
/**
 * 首页热门推荐
 * @author chenzb
 *
 */
class ctl_book_hot
{
    public static $arr_status = array(0=>'禁用',1=>'启用');

	function __construct()
	{
	}

    /**
     * 列表
     */
    public function index()
    {
        //初始
        $arr_status = self::$arr_status;
        tpl::assign('arr_status',$arr_status);
        $arr_type = mod::arr_type();
        tpl::assign('arr_type',$arr_type);
        // 组装A-where
        $where = array();
        $whereArr['type'] = req::item('type', '');
        $whereArr['book_id'] = req::item('book_id', '');
        foreach ($whereArr as $key => $value)
        {
            $value && $where[] = " `{$key}`='$value'";
        }
        // 组装B-where
        $whereArr['start_date'] = req::get('start_date', '');
        $whereArr['end_date']   = req::get('end_date', '');
        if ($whereArr['start_date'] && $whereArr['end_date'])
        {
            $where[] = "`start_date` between '{$whereArr['start_date']}' and '{$whereArr['end_date']}' ";
        }

        $where = empty($where) ? '' : ' ' . implode(' AND ', $where);
        $order    = "`sort` desc";

        // 数据
        list($list,$pages) = mod::get_page($where, 'book_hot', $order);

        // 处理
        if($list)foreach($list as $k=>$v)
        {
            $list[$k]['type_name'] = $arr_type[$v['type']];
            $list[$k]['status_name'] = $arr_status[$v['status']];
        }
        // 渲染
        tpl::assign('whereArr', $whereArr);
        tpl::assign('list', $list);
        tpl::assign('pages', $pages);
        tpl::display('book_hot.index.tpl');

    }
    /**
     * 新增
     */
    public function add()
    {
        if (req::$posts)
        {
            $data['img_url'] = mod_upload::move_img(req::post('img_url', '')) ? req::post('img_url', '') : '';
            $data['go_url'] = req::post('go_url', '');
            $data['sort'] = req::post('sort', 0, 'int');
            $data['start_date'] = req::post('start_date', '', 'text');
            $data['end_date'] = req::post('end_date', '', 'text');

            $data['status'] = req::post('status', 0, 'int');
            $data['type'] = req::post('type', 0, 'int');
            $data['book_id'] = req::post('book_id', 0, 'int');
            $data['book_name'] = req::post('book_name', 0, 'text');
            db::insert('book_hot', $data);
            $gourl = req::item('gourl');
            cls_access::show_message ('管理', '新增成功', $gourl);
        }
        else
        {
            $gourl = empty($_SERVER['HTTP_REFERER']) ? '?ct=book_hot&ac=index' : $_SERVER['HTTP_REFERER'];
            tpl::assign('arr_status', self::$arr_status);
            tpl::assign('arr_type', mod::arr_type());
            tpl::assign('gourl', $gourl);
            tpl::display('book_hot.add.tpl');
        }
    }

    /**
     * 新增2 - 通过booklist的按钮添加
     */
    public function add_for_book()
    {

    }

    /**
     * 编辑
     */
    public function edit()
    {
        $id = req::item('id', 0, 'int');
        if (req::$posts)
        {
            if(mod_upload::move_img(req::post('img_url', '')))//修改图片才更新
            {
                $data['img_url'] = req::post('img_url', '');
            }
            $data['img_url'] = mod_upload::move_img(req::post('img_url', '')) ? req::post('img_url', '') : '';
            $data['go_url'] = req::post('go_url', '');
            $data['sort'] = req::post('sort', 0, 'int');
            $data['start_date'] = req::post('start_date', '', 'text');
            $data['end_date'] = req::post('end_date', '', 'text');
            $data['status'] = req::post('status', 0, 'int');
            $data['type'] = req::post('type', 0, 'int');
            $data['book_id'] = req::post('book_id', 0, 'int');
            $data['book_name'] = req::post('book_name', 0, 'text');

            db::update('book_hot', $data, "`id`={$id}");
            $gourl = req::item('gourl');
            cls_access::show_message ('管理', '修改成功', $gourl);
        }
        else
        {
            $sql = "SELECT * FROM `book_hot` WHERE `id`='{$id}'";
            $banner = db::get_one($sql);
            tpl::assign('v', $banner);
            $gourl = empty($_SERVER['HTTP_REFERER']) ? '?ct=book_hot&ac=edit' : $_SERVER['HTTP_REFERER'];
            tpl::assign('arr_status', self::$arr_status);
            tpl::assign('arr_type', mod::arr_type());
            tpl::assign('gourl', $gourl);
            tpl::display('book_hot.edit.tpl');
        }
    }

    /**
     * 删除
     */
    public function del()
    {

    }


}
